package Control;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger; 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Bean.BeanUsuario;
import DAO.DAOUsuario;


public class ControladorUsuario extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, Exception {
		
		response.setContentType("text/html;charset=ISO-8859-1");
        PrintWriter out = response.getWriter();
    
        try {
           String opcao = request.getParameter("opcao");
           HttpSession session = request.getSession();
           if (session == null) {
        	   response.sendRedirect("../AcessoAoSistema/login.jsp"); 
           }else{
        	   if (opcao != null) {
        		   if (opcao.equals("logar")){
        			   BeanUsuario bean = (BeanUsuario) request.getAttribute("beanUsuario");
        			   this.controleValidarUsuario(bean.getLogin(), bean.getSenha(),response);
        		   } else response.sendRedirect("../paginaPrincipal.jsp");
        	   }	   
           }
        } catch(Exception e ){
            System.out.println(e.getLocalizedMessage());
        } finally {
            out.close();
        }
    }
    
    private void controleValidarUsuario(String login,String senha,HttpServletResponse response) throws Exception{
    	DAOUsuario dao = new DAOUsuario();
    	if (dao.validarUsuario(login, senha)) {
    		response.sendRedirect("../paginaPrincipal.jsp");
    	} else response.sendRedirect("../AcessoAoSistema/login.jsp");	
    }
    	
    	
    

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception e ) {
            Logger.getLogger(ControladorUsuario.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    /**response.sendRedirect("jsp/livros/cadastroLivro.jsp");
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception e ) {
            Logger.getLogger(ControladorUsuario.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
